package sqlanalysisgui.view.handler;

import org.apache.log4j.Logger;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

import sqlanalysisgui.editor.DashboardMultiPage;
import sqlanalysisgui.editor.DashboardTableInput;
import sqlanalysisgui.session.SessionInfo;
import sqlanalysisgui.view.ProjectListView;
import sqlanalysisgui.view.model.ProjectInfo;

public class DashboardGraphNodeHandler extends AbstractHandler {

	public final static String ID = "sqlanalysisgui.view.handler.DashboardGraphNodeHandler";

	private Logger logger = Logger.getLogger(this.getClass());

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		logger.info("프로젝트 클릭!!!");
		// Get the view
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = window.getActivePage();
		ProjectListView view = (ProjectListView) page
				.findView(ProjectListView.ID);
		// Get the selection
		ISelection selection = view.getSite().getSelectionProvider()
				.getSelection();
		if (selection != null && selection instanceof IStructuredSelection) {
			Object obj = ((IStructuredSelection) selection).getFirstElement();
			// If we had a selection lets open the editor
			if (obj != null) {
				ProjectInfo projectInfo = (ProjectInfo) obj;
				SessionInfo.setProjectInfo(projectInfo);
				DashboardTableInput input = new DashboardTableInput(projectInfo);
				SessionInfo.setProjectInfo(projectInfo);
				try {
					logger.info("에디터 페이지 열기");
					page.openEditor(input, DashboardMultiPage.ID);// 에디터 페이지 열기
//					page.openEditor(input, DashboardTable.ID);// 에디터 페이지 열기
				} catch (PartInitException e) {
					throw new RuntimeException(e);
				}
			}
		}
		return null;
	}

}
